package com.sdgakj.utils;

import org.apache.shiro.SecurityUtils;

import com.sdgakj.entity.User;
import com.sdgakj.service.account.ShiroDbRealm.ShiroUser;
import com.sdgakj.service.account.ShiroToken;

public class LoginAdminUtil {

	/**
	 * 取出Shiro中的当前用户Id.
	 */
	public static Long getCurrentUserId() {
		ShiroUser user = (ShiroUser) SecurityUtils.getSubject().getPrincipal();
		return user.id;
	}

	/**
	 * 取出Shiro中的当前用户Id.
	 */
	public static ShiroUser getCurrentUser() {
		ShiroUser user = (ShiroUser) SecurityUtils.getSubject().getPrincipal();
		return user;
	}
	
	/**
	 * 登录
	 * @param user
	 * @param rememberMe
	 * @return
	 */
	public static ShiroUser login(User user,Boolean rememberMe){
		ShiroToken token = new ShiroToken(user.getLoginName(), user.getPassword());
		token.setRememberMe(rememberMe);
		SecurityUtils.getSubject().login(token);
		return getCurrentUser();
	}
}
